package com.example.iot.controller;

import com.example.iot.entity.AppUpgrade;
import com.example.iot.entity.PatchUpgrade;
import com.example.iot.service.impl.AppUpgradeServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/appupgrade")
@RequiredArgsConstructor
@Component
public class APPUpgradeController {
    private final AppUpgradeServiceImpl appUpgradeService;
    @PostMapping("/appupgrade")
    public void patchUpgrade (@RequestBody AppUpgrade appUpgrade) {
        AppUpgrade appUpgrade1=appUpgradeService.selectByDevID(appUpgrade.getDeviceid(),appUpgrade.getAppname());
        if(appUpgrade1!=null){
            appUpgrade.setId(appUpgrade1.getId());
            appUpgradeService.updateById(appUpgrade);
        }else{
            appUpgradeService.save(appUpgrade);
        }
    }
    @GetMapping("/deviceverify")
    public List<AppUpgrade> selectByDevID (String deviceId) {
        List<AppUpgrade> appUpgrade=appUpgradeService.selectByDeviceID(deviceId);
        if(appUpgrade!=null){
            List<AppUpgrade> appUpgrades1=new ArrayList<>();
            for (AppUpgrade item: appUpgrade) {
                if(item.getIsipgrade()==2){
                    appUpgrades1.add(item);
                }
            }
            return appUpgrades1;
        }
        return null;

    }
}
